<?php
/*
 * Created on
 * Date: 2007-12-4
 * Author: William Yang
 */
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(DB_SESSION_DIR_PATH."/db_operator.php");
require_once(DB_SESSION_DIR_PATH."/db_obj_defs.php");

class MatchRequestManager extends MatchRequest {
	public function register() {
		$stat = parent::register(CONFIGURATION_DIR_PATH."/create_match_request.xml",
								FALSE);
		
		/*
		if ($stat === 0)
			return MatchReqRegStat::OK;
		else
			return MatchReqRegStat::INTERNAL_ERROR;
		*/
		MatchManager::increase_hot_index($this->matchid);
		return $stat;
	}
	
	public function query() {
		$db_opera = new DatabaseOperator();
		$db_opera->config(CONFIGURATION_DIR_PATH."/match_request_table.xml");
		$cList = new SqlCondList();
		if ($this->teamid != NULL)
			$cList->add_condition(new SqlCondition("teamid", SqlCondition::EQUAL, $this->teamid));
		if ($this->matchid != NULL) {
			if ($this->teamid != NULL)
				$cList->add_relation(SqlCondition::_AND);
			$cList->add_condition(new SqlCondition("matchid", SqlCondition::EQUAL, $this->matchid));
		}
		if ($this->status != NULL) {
			if ($this->teamid != NULL || $this->matchid != NULL)
				$cList->add_relation(SqlCondition::_AND);
			$cList->add_condition(new SqlCondition("status", SqlCondition::EQUAL, $this->status));
		}	
		
		$rows = $db_opera->select($cList);
		
		$ret_list = array();
		$delegate = new MatchRequest();
		foreach ($rows as $row) {
			$ret_list[] = $delegate->array2copy($row);
		}
		return $ret_list;
	}
	
	public static function query_by_match_uid($match_uid) {
		if ($match_uid == NULL) {
			return array();
		}
		
		$db_opera = new DatabaseOperator();
		$db_opera->config(CONFIGURATION_DIR_PATH."/match_request_table.xml");
		$cList = new SqlCondList();
		if (gettype($match_uid) === gettype(array())) {
			$cnt = count($match_uid);
			for ($i = 0; $i < $cnt - 1; ++$i) {
				$cList->add_condition(new SqlCondition('matchid', SqlCondition::EQUAL, $match_uid[$i]));
				$cList->add_relation(SqlCondition::_OR);
			}
			$cList->add_condition(new SqlCondition('matchid', SqlCondition::EQUAL, $match_uid[$cnt - 1]));
		} else
			$cList->add_condition(new SqlCondition('matchid', SqlCondition::EQUAL, $match_uid));
		
		$rows = $db_opera->select($cList);
		
		$ret_list = array();
		$delegate = new MatchRequest();
		foreach ($rows as $row) {
			$ret_list[] = $delegate->array2copy($row);
		}
		return $ret_list;
	}
	
	public static function query_by_team_uid($team_uid) {
		if ($team_uid == NULL) {
			return array();
		}
		
		$db_opera = new DatabaseOperator();
		$db_opera->config(CONFIGURATION_DIR_PATH."/match_request_table.xml");
		$cList = new SqlCondList();
		$cList->add_condition(new SqlCondition("teamid", SqlCondition::EQUAL, $team_uid));
		
		$rows = $db_opera->select($cList);
		
		$ret_list = array();
		$delegate = new MatchRequest();
		foreach ($rows as $row) {
			$ret_list[] = $delegate->array2copy($row);
		}
		return $ret_list;
	}
}
?>
